package com.level.restful.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * @author
 * 调用HIS获取数据
 *
 */
public class HttpUtil {


    private static int SUCCESS = 200;

    public static void main(String[] args) throws Exception{
        String targetURL = "http://122.112.203.146:8011/Api/xml/0001";
        String xml = "<Request><FunCode>2201</FunCode><CardNo>0001234567</CardNo><CardType>0</CardType><GhType>1</GhType><GhFlag>1</GhFlag><Date /><UserID>YYZZ</UserID></Request>";
        String deps =  HttpUtil.postDataForXml(targetURL, xml);
        System.out.println("deps="+deps);

    }

    /**
     *
     * @param posturl  调用的URL: http://122.112.203.146:8011/Api/Xml/医院代码/功能号
     * @param patString 入参xml或者Json
     * @T 出参对象类型
     **/
    public static String postDataForXml(String posturl, String patString) throws Exception{
        StringBuffer output = new StringBuffer();
        OutputStream outputStream = null;
        try {
            URL url = new URL(posturl);
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            outputStream = httpConnection.getOutputStream();
            outputStream.write(patString.getBytes());
            outputStream.flush();

            if (httpConnection.getResponseCode() != SUCCESS) {
                throw new RuntimeException("Failed : HTTP error code : " + httpConnection.getResponseCode());
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream()),"UTF-8"));

            String line = null;
            while ((line = responseBuffer.readLine()) != null) {
//                output += line;
                output.append(line);
            }

            httpConnection.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();
            throw e;

        } catch (IOException e) {

            e.printStackTrace();
            throw e;

        }finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
        return output.toString();

    }

    /**
     * 通过json格式获取HIS信息
     * @param posturl 请求地址服务：调用的URL: http://122.112.203.146:8011/Api/Json/医院代码/功能号
     * @param json 入参json字符串
     * @return 回参：json字符串
     */
    public static String postDateForJson(String posturl, String json) throws Exception{
        String output = null;
        OutputStream outputStream = null;
        try {
            URL url = new URL(posturl);
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            outputStream = httpConnection.getOutputStream();
            outputStream.write(json.getBytes());
            outputStream.flush();

            if (httpConnection.getResponseCode() != SUCCESS) {
                throw new RuntimeException("Failed : HTTP error code : " + httpConnection.getResponseCode());
            }
            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream())));

            output = responseBuffer.readLine();
            httpConnection.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
        return output;

    }
}
